import SnowflakeId from "snowflake-id";
const { Op } = require("sequelize");
const EntModels = require('../models/ent');

interface enterprise {
    id?: number
    address?: string,
    city: number,
    introduction?: string,
    name: string,
    sname?: string,
    uscc?: string
}

class CourseService {
    // 获取企业列表
    public async getEnts() {
        const res: any = await EntModels.findAndCountAll()
        return res ? res : null
    }
    // 分页获取企业列表
    public async getEntsByPage(pageNum: number, pageSize: number) {
        const offset = (pageNum - 1) * pageSize
        const { count, rows } = await EntModels.findAndCountAll({
            offset: offset,
            limit: pageSize * 1
        })
        return {
            pageNum,
            pageSize,
            total: count,
            list: rows
        }
    }
    // 根据关键词搜索企业列表
    public async getByname(name: string) {
        // 不分页直接获取
        const { count, rows } = await EntModels.findAndCountAll({
            where: {
                [Op.or]: [
                    {
                        name: {
                            [Op.like]: `%${name}%`,
                        }
                    },
                    {
                        sname: {
                            [Op.like]: `%${name}%`,
                        }
                    }
                ]
            }
        })
        return {
            total: count,
            list: rows
        }
    }
    // 分页获取-根据关键词搜索企业列表
    public async getBynameandPage(pageNum: number, pageSize: number, name: string) {
        const offset = (pageNum - 1) * pageSize
        const { count, rows } = await EntModels.findAndCountAll({
            where: {
                [Op.or]: [
                    {
                        name: {
                            [Op.like]: `%${name}%`,
                        }
                    },
                    {
                        sname: {
                            [Op.like]: `%${name}%`,
                        }
                    }
                ]
            },
            offset: offset,
            limit: pageSize * 1
        })
        return {
            pageNum,
            pageSize,
            total: count,
            list: rows
        }
    }
    // 根据id获取企业
    public async getEntsById(id: number) {
        const res = await EntModels.findOne({
            where: {
                id
            }
        })
        return res ? res.dataValues : null
    }
    // 添加企业
    public async createEnt(params: enterprise) {
        const res: any = await EntModels.create(params)
        return res ? res.dataValues : null;
    }
    // 更新企业
    public async update(id: number, params: enterprise) {
        const whereOpt = { id }
        const res = await EntModels.update(params, { where: whereOpt })
        return res[0] > 0
    }
    // 删除企业
    public async delEnt(id: number) {
        const res: any = await EntModels.destroy({
            where: { id }
        })
        return res > 0
    }
    // 批量导入企业
    public async importEnts(ents: Array<Object>) {
        const res = await EntModels.bulkCreate(ents);
        return res.length > 0
    }
}

module.exports = new CourseService()
// 解决同目录下多个ts文件都引入Op,会报错的问题
export { }